﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YJKRevitFamilyDataBase;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgEditCategory : System.Windows.Forms.Form
	{
		public static DlgEditCategory GetInstance(DlgMain main)
		{
			if (DlgEditCategory.dac == null || DlgEditCategory.dac.IsDisposed)
			{
				DlgEditCategory.dac = new DlgEditCategory(main);
				DlgEditCategory.dac.MdiParent = main.ActiveMdiChild;
			}
			return DlgEditCategory.dac;
		}

		public DlgEditCategory(DlgMain main)
		{
			this.mForm = main;
			this.InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				this.mForm.CurrentCategory.Database.CategoryTable.Update(this.mForm.CurrentCategory);
				DbCategory dbCategory = (DbCategory)this.cbxCat.SelectedItem;
				List<DbParamDefinition> list = new List<DbParamDefinition>();
				for (int i = 0; i < this.lbxRecordParam.Items.Count; i++)
				{
					DbParamDefinition dbParamDefinition = (DbParamDefinition)this.lbxRecordParam.Items[i];
					if (!dbParamDefinition.Limit)
					{
						list.Add(dbParamDefinition);
					}
				}
				this.mForm.CurrentSubject.Database.CategoryParamTable.Remove(dbCategory);
				this.mForm.CurrentSubject.Database.CategoryParamTable.Add(dbCategory, list);
				this.mForm.CurrentSubject.Database.CategoryTable.Update(dbCategory);
				this.mForm.trCategory.SelectedNode.Text = dbCategory.Name;
				base.Close();
			}
			catch
			{
				MessageBox.Show("更新失败，请检查分类是否重名");
			}
		}

		private void cbxSubject_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbxRecordParam_DrawItem(object sender, DrawItemEventArgs e)
		{
			if (e.Index >= 0)
			{
				e.DrawBackground();
				Brush brush = Brushes.Black;
				if (((DbParamDefinition)this.lbxRecordParam.Items[e.Index]).Limit)
				{
					brush = Brushes.Gray;
				}
				e.Graphics.DrawString(this.lbxRecordParam.Items[e.Index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
				e.DrawFocusRectangle();
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (this.tbxParamName != null)
			{
                DbParamDefinition dbParamDefinition = new DbParamDefinition(this.tbxParamName.Text, 0, ParamDefinitionType.CaregoryParam, (Autodesk.Revit.DB.BuiltInParameterGroup)(-1));
				if (!this.lbxRecordParam.Items.Contains(dbParamDefinition))
				{
					this.lbxRecordParam.Items.Add(dbParamDefinition);
					this.tbxParamName.Text = "";
					return;
				}
				MessageBox.Show("参数重名，请重新输入！", "族库管理");
				this.tbxParamName.Text = "";
			}
		}

		private void tbxParamName_TextChanged(object sender, EventArgs e)
		{
			if (new Regex("^\\w").Match(this.tbxParamName.Text).Success)
			{
				this.btnAdd.Enabled = true;
				return;
			}
			this.btnAdd.Enabled = false;
		}

		private void lbxRecordParam_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem == null)
			{
				this.btnDel.Enabled = false;
				return;
			}
			DbParamDefinition dbParamDefinition = (DbParamDefinition)this.lbxRecordParam.SelectedItem;
			this.propertyGrid1.SelectedObject = dbParamDefinition;
			this.btnDel.Enabled = true;
			if (dbParamDefinition.Limit)
			{
				this.propertyGrid1.Enabled = false;
				return;
			}
			this.propertyGrid1.Enabled = true;
		}

		private void cbxCat_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbxCat.SelectedItem != null)
			{
				DbCategory dbCategory = (DbCategory)this.cbxCat.SelectedItem;
				foreach (DbParamDefinition item in dbCategory.GetParamDefinitionList())
				{
					this.lbxRecordParam.Items.Add(item);
				}
				this.propertyGrid2.SelectedObject = dbCategory;
			}
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				this.cmdRemoveParam();
			}
		}

		private void cmdRemoveParam()
		{
			if (((DbParamDefinition)this.lbxRecordParam.SelectedItem).Limit)
			{
				MessageBox.Show("不能移除父级分类规定的属性", "族库管理");
				return;
			}
			int selectedIndex = this.lbxRecordParam.SelectedIndex;
			this.lbxRecordParam.Items.RemoveAt(selectedIndex);
		}

		private void DlgEditCategory_Load(object sender, EventArgs e)
		{
			if (this.mForm.trCategory.SelectedNode != null && this.mForm.trCategory.SelectedNode.Tag != null)
			{
				this.cbxSubject.Items.Add(this.mForm.CurrentSubject);
				this.cbxSubject.SelectedIndex = 0;
				if (this.mForm.CurrentCategory.ParentId == this.mForm.CurrentSubject.Guid)
				{
					this.cbxCategory.Items.Add(this.mForm.CurrentSubject);
				}
				else
				{
					this.cbxCategory.Items.Add(this.mForm.CurrentCategory.Database.CategoryTable[this.mForm.CurrentCategory.ParentId]);
				}
				this.cbxCategory.SelectedIndex = 0;
				this.cbxCat.Items.Add(this.mForm.CurrentCategory);
				this.cbxCat.SelectedIndex = 0;
			}
			this.lbxRecordParam.DoubleClick += this.lbxRecordParam_DoubleClick;
			this.lbxRecordParam.DrawItem += this.lbxRecordParam_DrawItem;
		}

		private void lbxRecordParam_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				this.cmdRemoveParam();
			}
		}

		private void propertyGrid2_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
		{
		}

		private void DlgEditCategory_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private static DlgEditCategory dac;

		private DlgMain mForm;
	}
}
